package com.jnu.bookstore;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.NonNull;

import com.jnu.bookstore.data.CirleSpriter;

import java.util.ArrayList;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mSurfaceHolder;
    //绘图的Canvas
    private Canvas mCanvas=null;
    //子线程标志位
    private DrawThred drawThread;
    private boolean isTouched;

    public GameView(Context context) {
        super(context);
        InitView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        InitView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        InitView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        InitView();
    }

    private void InitView()
    {
        mSurfaceHolder=getHolder();
        mSurfaceHolder.addCallback(this);
    }

public float touchedx;
    public float touchedy;
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        drawThread = new DrawThred();
        drawThread.start();
        GameView.this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(MotionEvent.ACTION_DOWN==event.getAction()){
                    touchedx=(float)event.getRawX();
                    touchedy=(float)event.getRawY();
                }
                return false;
            }
        });
    }



    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
            drawThread.mystop();
    }
    private boolean b=false;
    private ArrayList<CirleSpriter> spriters=new ArrayList<>();
    private class DrawThred extends Thread{

        public DrawThred(){
            spriters.add(new CirleSpriter(300,300,100,GameView.this.getWidth(),GameView.this.getHeight()));
            spriters.add(new CirleSpriter(100,100,100,GameView.this.getWidth(),GameView.this.getHeight()));
        }
        public void mystop(){
            b=true;
        }
        @Override
        public void run() {
            super.run();
            int hitCount=0;
            while (!b) {
                try {
                    mCanvas = mSurfaceHolder.lockCanvas();//获得画布
                    mCanvas.drawColor(Color.GRAY);
                    if(isTouched) {
                        for(int index=0;index<spriters.size();index++){
                            spriters.get(index).isShooted(GameView.this.touchedx,GameView.this.touchedy);
                            hitCount++;
                        }
                    }
                    for(int index=0;index<spriters.size();index++){
                        spriters.get(index).move();
                    }
                    for(int index=0;index<spriters.size();index++){
                       spriters.get(index).draw(mCanvas);
                    }
                    GameView.this.isTouched=false;
                    Paint paint=new Paint();
                    paint.setColor(Color.GREEN);
                    paint.setTextSize(100);
                    mCanvas.drawText("hit"+hitCount,100,100,paint);
                } catch (Exception e) {

                } finally {
                    {
                        if (mCanvas != null)
                            //释放canva对象并释放画布
                            mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                    }
                }
                try{
                    Thread.sleep(20);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
